﻿using AntdUI;
using AntdUI.In;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Utils;
using MySql.Data.MySqlClient;
using Mysqlx.Crud;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Pqc.Crypto.Lms;
using ScottPlot.MultiplotLayouts;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
using static BST250301TestStand.Models.Utils.PLCConfig;
using static BST250301TestStand.Models.Utils.TCUConfig;
using static System.ComponentModel.Design.ObjectSelectorEditor;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace BST250301.Views
{
    public partial class RecordPara : UserControl
    {
        private AntdUI.Window window;
        //private Target tag;
        private RecordData _item;
        public bool submit;
        //string currentArea = string.Empty;
        public RecordPara(RecordData item)
        {
            if (item != null)
                this._item = item;
            else
                this._item = new RecordData();
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            var result = DB.GetRecordData();
            selectName.Items.Clear();
            for (int i = 0; i < result.Count; i++)
            {
                selectName.Items.Add(result[i].Name);
            }

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            bool isnew = false;
            DialogResult confirmResult = MessageBox.Show("确认修改记录", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (confirmResult != DialogResult.Yes)
                return;
            this._item.Name = selectName.Text.Trim();
            this._item.Desc = inputDesc.Text.Trim();
            this._item.DataFrom = (RecordFrom)selectSource.SelectedIndex;
            this._item.DefaultValue = selectDefault.Text.Trim();
            this._item.Unit = selectUnit.Text.Trim();
            this._item.AppName = selectAppName.Text.Trim();
            this._item.ReportName = selectReportName.Text.Trim();

            float.TryParse(selectStandMax.Text.Trim(), out float standMax);
            this._item.StandardMax = standMax;
            float.TryParse(selectStandMin.Text.Trim(), out float standMin);
            this._item.StandardMin = standMin;

            var ret = DB.SaveRecordParameters(this._item.Name, this._item.ReportName, this._item.AppName, this._item.Desc, (int)this._item.DataFrom, this._item.DefaultValue, this._item.StandardMax.ToString(), this._item.StandardMin.ToString(), this._item.Unit);
            if (ret)
            {
                MessageBox.Show("保存成功");
                submit = true;
                this.Dispose();
            }
            else
                MessageBox.Show("保存出错，请检查");
        }


        private void button2_Click(object sender, EventArgs e)
        {
            submit = false;
            this.Dispose();
        }

        private void selectSource_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            selectAppName.Items.Clear();
            if (selectSource.SelectedIndex == 0)
            {
                Type typeT = typeof(ShowTCUStatusParm);
                PropertyInfo[] propertiesS = typeT.GetProperties();
                foreach (PropertyInfo prop in propertiesS)
                {
                    selectAppName.Items.Add(prop.Name);
                }
            }
            else if (selectSource.SelectedIndex == 1)
            {
                Type typeT = typeof(ShowPLCStatusParm);
                PropertyInfo[] propertiesS = typeT.GetProperties();
                foreach (PropertyInfo prop in propertiesS)
                {
                    selectAppName.Items.Add(prop.Name);
                }
            }
            else if (selectSource.SelectedIndex == 2)
            {
                Type typeT = typeof(ShowNVHStatusParm);
                PropertyInfo[] propertiesS = typeT.GetProperties();
                foreach (PropertyInfo prop in propertiesS)
                {
                    selectAppName.Items.Add(prop.Name);
                }
            }
        }

        private void selectName_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            var rec = DB.GetRecordDataByName(selectName.Text);
            if (rec != null)
            {
                var ret = Enum.GetValues(typeof(RecordFrom));
                foreach (var item in ret)
                {
                    selectSource.Items.Add(item);
                    if (this._item != null && this._item.DataFrom == (RecordFrom)item)
                        selectSource.SelectedIndex = (int)item;
                }
                this.selectName.Text = rec.Name;
                this.inputDesc.Text = rec.Desc;
                this.selectReportName.Text = rec.ReportName;
                this.selectUnit.Text = rec.Unit;
                this.selectDefault.Text = rec.DefaultValue;
                this.selectStandMax.Text = rec.StandardMax.ToString("F2");
                this.selectStandMin.Text = rec.StandardMin.ToString("F2");
                this.selectAppName.Text = rec.AppName;
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            DialogResult confirmResult = MessageBox.Show("确认删除？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (confirmResult != DialogResult.Yes)
                return;
            var ret = DB.DeleteRecordParameters(this.selectName.Text);
            if (ret)
            {
                MessageBox.Show("删除成功");
                submit = true;
                this.Dispose();
            }
            else
                MessageBox.Show("删除出错，请检查");
        }
    }
}
